package com.dci.service;

import com.dci.dao.pms.XtgOgzInfoMapper;
import com.dci.dto.XtgOgzLevelDto;
import com.dci.model.XtgOgzInfo;
import com.dci.util.LevelUtil;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * All rights Reserved, Designed By 姑苏区政府大数据中心
 *
 * @version V1.0
 * @Title: null.java
 * @Package com.dci.service
 * @Description: 计算树公用方法
 * @author: 张星亮
 * @date: 2019/2/25
 * @Copyright: 2019 姑苏区政府大数据中心. All rights reserved.
 * 注意：本内容仅限于姑苏区政府大数据中心内部传阅，禁止外泄以及用于其他的商业目
 */
@Service
public class XtgTreeService {

    public Comparator<XtgOgzLevelDto> ogzSeqComparator = new Comparator<XtgOgzLevelDto>() {
        @Override
        public int compare(XtgOgzLevelDto o1, XtgOgzLevelDto o2) {
            return o1.getSeq() - o2.getSeq();
        }
    };
    @Resource
    private XtgOgzInfoMapper xtgOgzInfoMapper;

    public List<XtgOgzLevelDto> ogzTree() throws Exception {
        // 获取所有部门信息
        List<XtgOgzInfo> ogzInfoList = xtgOgzInfoMapper.getAllOgz();
        List<XtgOgzLevelDto> ogzLevelDtoList = Lists.newArrayList();
        ogzInfoList.forEach(ogz -> {
            XtgOgzLevelDto dto = XtgOgzLevelDto.adapt(ogz);
            ogzLevelDtoList.add(dto);
        });
        return ogzListToTree(ogzLevelDtoList);
    }

    public List<XtgOgzLevelDto> ogzListToTree(List<XtgOgzLevelDto> dtoLevelList) {
        if (CollectionUtils.isEmpty(dtoLevelList)) {
            return Lists.newArrayList();
        }
        Multimap<String, XtgOgzLevelDto> levelDtoMultimap = ArrayListMultimap.create();
        // 拿出一级部门
        List<XtgOgzLevelDto> rootList = Lists.newArrayList();
        dtoLevelList.forEach(dto -> {
            levelDtoMultimap.put(dto.getLevel(), dto);
            if (LevelUtil.ROOT.equals(dto.getLevel())) {
                rootList.add(dto);
            }
        });
        // 按照seq的从小到大排序
        Collections.sort(rootList, new Comparator<XtgOgzLevelDto>() {
            @Override
            public int compare(XtgOgzLevelDto o1, XtgOgzLevelDto o2) {
                return o1.getSeq() - o2.getSeq();
            }
        });
        // 递归生成树
        transformOgzTree(rootList, LevelUtil.ROOT, levelDtoMultimap);
        return rootList;
    }

    public void transformOgzTree(List<XtgOgzLevelDto> ogzLevelDtoList, String level, Multimap<String, XtgOgzLevelDto> levelDtoMultimap) {
        for (int i = 0; i < ogzLevelDtoList.size(); i++) {
            // 遍历该层的每个元素
            XtgOgzLevelDto xtgOgzLevelDto = ogzLevelDtoList.get(i);
            // 处理当前层级的数据
            String nextLevel = LevelUtil.calculateLevel(level, xtgOgzLevelDto.getId());
            // 处理下一层
            List<XtgOgzLevelDto> tempOgzList = (List<XtgOgzLevelDto>) levelDtoMultimap.get(nextLevel);
            if (!(CollectionUtils.isEmpty(tempOgzList))) {
                // 排序
                Collections.sort(tempOgzList, ogzSeqComparator);
                // 设置下一层部门
                xtgOgzLevelDto.setXtgOgzList(tempOgzList);
                // 处理下一层
                transformOgzTree(tempOgzList, nextLevel, levelDtoMultimap);
            }
        }
    }

}
